//
//  HMMainViewController.swift
//  Weibo
//
//  Created by 黄亦寒 on 16/9/19.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
import SVProgressHUD
class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //把系统tabbar替换为自定义tabbar
        let tabbar = HMTabBar()
        tabbar.composeButtonClosure = { [weak self] in
            print("我是闭包")
            
            if !HMUserAccountViewModel.sharedHMUserAccountViewModel.isLogin {
                SVProgressHUD.showError(withStatus: "请先登录，亲~")
                return
            }
            
            if let target = self {
                //代码执行到此说明已登录
                let composeView = HMComposeView()
//                let window = UIApplication.shared.windows.last!
//                window.addSubview(composeView)
                composeView.show(target: target)
            }
            
            
            
        }
        
        
        //5.设置代理
        tabbar.hmDelegate = self
        
        setValue(tabbar, forKey: "tabBar")
        
        addChildViewController(childController: HMHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: HMMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: HMDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: HMProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
    }

    
    //添加子视图控制器的方法
    func addChildViewController(childController:UIViewController,title:String,imageName:String) {
        //设置tabbarItem的图片
        childController.tabBarItem.image = UIImage(named:imageName)
        //修改图片的渲染方式
        childController.tabBarItem.selectedImage = UIImage(named:"\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        //设置图片选中时的文字颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        //设置文字大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont .systemFont(ofSize: 14)], for: .normal)
        //设置图片文字和标题文字
        childController.title = title
        //创建导航控制器
        let nav = HMNavigationViewController(rootViewController:childController)
        //添加子视图控制器
        addChildViewController(nav)
        
    }
   
}

//6.实现代理方法
extension HMMainViewController:HMTabBarDelegate {
    func didSelectedComposeButton() {
        print("我是代理")
    }
}
